/* RSSocket - Decompiled by JODE
 * Visit http://jode.sourceforge.net/
 */
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.Socket;

public class RSSocket implements Runnable {
	public int currentOffset = 0;
	public InputStream inputStream;
	public Socket underlyingSocket;
	public Signlink signlink;
	public static BigInteger aBigInteger292 = (new BigInteger("7162900525229798032761816791230527296329313291232324290237849263501208207972894053929065636522363163621000728841182238772712427862772219676577293600221789"));
	public OutputStream outputStream;
	public boolean threadDead = false;
	public Resource activeThread;
	public static JString aClass3_296 = Class39_Sub5_Sub9.createJString(109, "Chat panel redrawn");
	public byte[] buffer;
	public static JString aClass3_298;
	public static JString aClass3_299;
	public static JString aClass3_300;
	public static int anInt301 = 128;
	public static int anInt302;
	public int writeOffset = 0;
	public static Deque aClass49_304;
	public static JString aClass3_305;
	public static JString aClass3_306;
	public boolean close = false;
	public static JString aClass3_308;
	public static int[] anIntArray309;
	public static JString aClass3_310;
	public static JString aClass3_311;
	public static CacheArchive aClass9_Sub1_312;
	public static int anInt313;

	public static void method216(int i) {
		aClass3_299 = null;
		aClass49_304 = null;
		aBigInteger292 = null;
		aClass9_Sub1_312 = null;
		anIntArray309 = null;
		aClass3_296 = null;
		aClass3_311 = null;
		aClass3_298 = null;
		aClass3_310 = null;
		aClass3_308 = null;
		aClass3_300 = null;
		aClass3_306 = null;
		aClass3_305 = null;
	}

	public void run() {
		try {
			for (;;) {
				int i;
				int i_0_;
				synchronized (this) {
					if (currentOffset == writeOffset) {
						if (threadDead)
							break;
						try {
							this.wait();
						} catch (InterruptedException interruptedexception) {
							/* empty */
						}
					}
					i = currentOffset;
					if (writeOffset < currentOffset)
						i_0_ = -currentOffset + 5000;
					else
						i_0_ = -currentOffset + writeOffset;
				}
				if (i_0_ > 0) {
					try {
						outputStream.write(buffer, i, i_0_);
					} catch (IOException ioexception) {
						close = true;
					}
					currentOffset = (currentOffset + i_0_) % 5000;
					try {
						if (writeOffset == currentOffset)
							outputStream.flush();
					} catch (IOException ioexception) {
						close = true;
					}
				}
			}
			try {
				if (inputStream != null)
					inputStream.close();
				if (outputStream != null)
					outputStream.close();
				if (underlyingSocket != null)
					underlyingSocket.close();
			} catch (IOException ioexception) {
				/* empty */
			}
			buffer = null;
		} catch (Exception exception) {
			Class39_Sub7.method849(exception, 64, null);
		}
	}

	public void read(int length, byte[] dest, int offset) throws IOException {
		if (!threadDead) {
			int i_3_;
			for (/**/; length > 0; length -= i_3_) {
				i_3_ = inputStream.read(dest, offset, length);
				if (i_3_ <= 0)
					throw new EOFException();
				offset += i_3_;
			}
		}
	}

	public void killThread(int i) {
		if (!threadDead) {
			synchronized (this) {
				if (i != -2158)
					run();
				threadDead = true;
				this.notifyAll();
			}
			if (activeThread != null) {
				while (activeThread.state == 0)
					Class45.method917(3, 1L);
				if (activeThread.state == 1) {
					try {
						((Thread) activeThread.resource).join();
					} catch (InterruptedException interruptedexception) {
						/* empty */
					}
				}
			}
			activeThread = null;
		}
	}

	public static boolean method219(int i, int i_4_) {
		if (Renderable.aBooleanArray1727[i_4_])
			return true;
		if (!Class39_Sub5_Sub16.aClass9_1979.method164(false, i_4_))
			return false;
		int i_5_ = Class39_Sub5_Sub16.aClass9_1979.method158(i_4_, (byte) -4);
		if (i_5_ == 0) {
			Renderable.aBooleanArray1727[i_4_] = true;
			return true;
		}
		if (Class62_Sub1.interfaceCache[i_4_] == null)
			Class62_Sub1.interfaceCache[i_4_] = new RSInterface[i_5_];
		for (int i_6_ = i; i_6_ < i_5_; i_6_++) {
			if (Class62_Sub1.interfaceCache[i_4_][i_6_] == null) {
				byte[] is = Class39_Sub5_Sub16.aClass9_1979.method165(30605, i_4_, i_6_);
				if (is != null) {
					Class62_Sub1.interfaceCache[i_4_][i_6_] = new RSInterface();
					Class62_Sub1.interfaceCache[i_4_][i_6_].anInt2084 = i_6_ + (i_4_ << 16);
					if (is[0] == -1)
						Class62_Sub1.interfaceCache[i_4_][i_6_].unpackInterface(new Stream(is), false);
					else
						Class62_Sub1.interfaceCache[i_4_][i_6_].method755(new Stream(is), 4);
				}
			}
		}
		Renderable.aBooleanArray1727[i_4_] = true;
		return true;
	}

	public int read() throws IOException {
		if (threadDead)
			return 0;
		return inputStream.read();
	}

	public void finalize() {
		killThread(-2158);
	}

	public int available() throws IOException {
		if (threadDead)
			return 0;
		return inputStream.available();
	}

	public void write(int dummy, byte[] bytes, int length, int offset) throws IOException {
		if (!threadDead) {
			if (close) {
				close = false;
				throw new IOException();
			}
			if (buffer == null)
				buffer = new byte[5000];
			synchronized (this) {
				int i_9_ = 0;
				if (dummy != -1) {
					/* empty */
				} else {
					for (/**/; length > i_9_; i_9_++) {
						buffer[writeOffset] = bytes[offset + i_9_];
						writeOffset = (writeOffset + 1) % 5000;
						if (writeOffset == (currentOffset + 4900) % 5000)
							throw new IOException();
					}
					if (activeThread == null)
						activeThread = signlink.method252(-118, this, 3);
					this.notifyAll();
				}
			}
		}
	}

	public RSSocket(Socket socket, Signlink class21) throws IOException {
		signlink = class21;
		underlyingSocket = socket;
		underlyingSocket.setSoTimeout(30000);
		underlyingSocket.setTcpNoDelay(true);
		inputStream = underlyingSocket.getInputStream();
		outputStream = underlyingSocket.getOutputStream();
	}

	static {
		aClass3_298 = Class39_Sub5_Sub9.createJString(103, "Error connecting to server)3");
		aClass3_299 = aClass3_298;
		aClass3_300 = Class39_Sub5_Sub9.createJString(107, "und Ihr Passwort ein)3");
		aClass3_306 = Class39_Sub5_Sub9.createJString(114, "Username: ");
		aClass3_305 = aClass3_306;
		aClass49_304 = new Deque();
		aClass3_310 = Class39_Sub5_Sub9.createJString(121, "runes");
		aClass3_311 = Class39_Sub5_Sub9.createJString(108, "cyan:");
		anInt313 = 0;
		aClass3_308 = aClass3_311;
	}
}
